#!/bin/sh

# compares with lurette the results of:
#   lus2lic -ec and ecexe
#   lus2lic -exec

echo "================> run in `pwd`: $0 $@"

lustre_file=$1
shift
OPT=$@
node=`basename $lustre_file .lus`
_oracle=_"$node"_oracle.lus
oracle="$node"_oracle
lv4="$node"_lv4.lus
ec="$node".ec
lv4_node="$node__$node"
env=_"$node"_env.lut
env_node="$node"_env


set -x verbose #echo on

if
    [ -f $env_node ];
then
   echo "$env_node generated"

else if
     ./lus2lic $OPT $lustre_file -n $node --gen-autotest -np &&
     ./lus2lic $OPT $lustre_file -n $node -np -lic -o $lustre_file.lic &&
     cat $lustre_file.lic  >> $_oracle;
then
     echo "./lus2lic $OPT $lustre_file -n $node --gen-autotest -np: ok"
else
    echo "Error"
    exit 2
fi
fi


if
    ./lus2lic $lustre_file -n $node  -ec  -o $ec;
#    ./lus2lic $lustre_file -n $node  -en -lv4 -eei --no-prefix -o $lv4;
#    lus2ec $lv4 $node
then
    echo "lus2lic -ec done"
else
    echo "lus2lic $lustre_file -n $node -ec -o $ec: returned an error"
    exit 2
fi


. /home/jahier/.opam/opam-init/variables.sh
export PATH=/usr/local/tools/lustre/v4/bin/:$PATH
#  if
#      ./lus2lic $_oracle -n $oracle  -ec -o $oracle.ec
#  #    ./lus2lic  lv4$_oracle -n $oracle -ec -o $oracle.ec
#  #    lus2ec  $_oracle  $oracle  -o $oracle.ec;
#  then
#  echo "ec generation done"
#  else
#      echo "Error"
#      exit 2
#  fi
if
#    -rp "sut:v4:$lv4:$lv4_node" \

echo "./lus2lic -2c-exec $_oracle -n $oracle $OPT --expand-io-type" > $_oracle.sh

     PRECISION=4
     ./lus2lic -2c $_oracle -n $oracle $OPT &&
     lurette -l 10   \
         -sut "ecexe-rif $ec $lv4_node -rif" \
         -env "lutin $env -n $env_node -p $PRECISION" \
         -oracle "sh $_oracle.sh"

# Do not use the lurette-embedded lv6 interpreter to avoid spurious float rounding errors.
# Indeed, the lurette-embedded lv6 interpreter uses the exact value generated by lutin.
#        -oracle "./lus2lic $_oracle -n $oracle $OPT"


# lurettetop_exe -p $PRECISION -rp "sut:ec:$ec:$lv4_node" -rp "env:lutin:$env:-p:$PRECISION"  -rp "oracle:v6:$_oracle:$oracle:$OPT" -go -l 10 -ns2c --stop-on-oracle-error;
then
     echo "lurettetop ok"
else
     echo "lurette returned an error"
     exit 2
fi
echo "<================ DONE (run in `pwd`: $0)"
exit 0
